#include <string.h>
#include <stdio.h>
#include "stdlib.h"
#include "test.h"

#define length(x) (sizeof(x) / sizeof *(x))

static struct {
    char *s;
    double f;
    int ndigit;
} t[] = {
    {"0", 0.0, 0},
    {"0", 0.0, 2},
    {"-0", -0.0, 5},
    {"0.59618603481318067", 0.59618603481318067, 17}, 
    {"0.5961860348131807", 0.59618603481318067, 16}, 
    {"0.596186034813181", 0.59618603481318067, 15}, 
    {"0.59618603481318", 0.59618603481318067, 14}, 
    {"0.6", 0.59618603481318067, 1}, 
    {"1.181501316921804", 1.1815013169218038, 16}, 
    {"42.4207082357534", 42.420708235753445, 15}, 
    {"665.66546863065", 665.66546863065162, 14}, 
    {"6101.852922971", 6101.8529229708685, 13}, 
    {"76966.9520824", 76966.952082369677, 12}, 
    {"250506.53222", 250506.53222286823, 11}, 
    {"2740037.23", 2740037.2302280052, 10}, 
       {"-2740037.23", -2740037.2302280052, 10}, 
    {"20723093.5", 20723093.500497428, 9},
       {"-20723093.5", -20723093.500497428, 9},
};

int gcvt_test(void)
{
    int i;
    double x;
    char str[20];
    
    for (i = 0; i < length(t); i++) 
    {
        gcvt(t[i].f, t[i].ndigit, str);
        if (0 != strcmp((const char*)t[i].s, (const char*)str) )
            t_error("gcvt(\"%f\") want %s got %s\n", x, t[i].s, str); 
    }
    return t_status;
}